<?php

/**
 * 文档模型 - 模型类
 * 
 * @package    01CMS
 * @subpackage admin
 * @author     rolong at vip.qq.com
 * @version    1.0.0
 * @link       http://www.01cms.com
 */

class ChannelModel
{
    public $id;
    public $data;
    
    /**
     * 调用getAddData()方法后，文档分页内容的总页数会缓存到这个属性
     *
     * @var int
     */
    public $countContentPages;

    function __construct ($id = 0)
    {
        if ($id > 0)
        {
            $channel = $this->getData();
            
            $this->id = $id;
            $this->data = isset($channel[$id]) ? $channel[$id] : array();
        }
    }

    function getData ()
    {
        if (data('archiveChannel') === FALSE)
        {
            $this->writeChannel();
        }
        
        return data('archiveChannel');
    }

    /**
     * 获取模型附加字段
     * 
     * @return object
     * @access public
     */
    function getAddFields ()
    {
        return model('Db')->getRows("SELECT * FROM @@__archive_field where channelId={$this->id}");
    }

    /**
     * 获取文章的型模型附加内容
     * 
     * @param int $archiveId
     * @return array
     * @access public
     */
    function getAddData ($archiveId, $page = 1)
    {
        $data = model('Db')->getRows("SELECT * FROM @@__archive_channel_{$this->tableId} where archiveId={$archiveId}");
        
        if ($page != 1)
        {
            $channelTables = explode(',', Base::i()->Load->var['pagesContentChannel']);
            
            $isPages = in_array($this->tableId, $channelTables) ? 1 : 0;
            
            if ($isPages)
            {
                $this->countContentPages = count($data);
                
                if ($this->countContentPages > 1 && $page == - 1)
                {
                    $pageDelimiter = addslashes(Base::i()->Load->var['pageDelimiter']);
                    
                    for ($i = 1; $i < $this->countContentPages; $i ++)
                    {
                        $data[0]->content .= $pageDelimiter . $data[$i]->content;
                    }
                }
                else if ($page > 1)
                {
                    $page = ($page > $this->countContentPages) ? $this->countContentPages : $page;
                    
                    $data[0]->content = $data[$page]->content;
                }
            }
        }
        
        if (empty($data))
        {
            showError('无法读取模型附加数据');
        }
        
        return $data[0];
    }

    function delete ()
    {
        $Db = model('Db');
        
        $categories = $Db->getRows('SELECT id FROM @@__archive_category WHERE channelId = ' . $this->id);
        
        foreach ($categories as $category)
        {
            $Db->delete("categoryId = {$category->id}", '@@__archive_archive');
        }
        
        $Db->delete("channelId = {$this->id}", '@@__archive_category');
        $Db->delete("channelId = {$this->id}", '@@__archive_field');
        $Db->delete("id = {$this->id}", '@@__archive_channel');
        
        $Db->query("DROP TABLE `@@__archive_channel_{$this->tableId}`;");
        
        $this->writeChannel();
    }

    /**
     * 文档模型数据写入文件
     */
    function writeChannel ()
    {
        $Db = model('Db');
        
        $channels = $Db->getRows('SELECT * FROM @@__archive_channel');
        
        $channelsData = array();
        
        foreach ($channels as $channel)
        {
            $categories = $Db->getRows("SELECT id FROM @@__archive_category WHERE channelId = {$channel->id}");
            $categoryIds = '';
            foreach ($categories as $category)
            {
                $categoryIds .= empty($categoryIds) ? $category->id : ',' . $category->id;
            }
            $channelsData[$channel->id] = array('name'=>$channel->name, 'tableId'=>$channel->tableId, 'fieldList'=>$channel->fieldList, 'categoryIds'=>$categoryIds);
        }
        
        lib('Write')->phpArray(SYS_PATH . '/data/archiveChannel.php', $channelsData);
    }

    /**
     * 更新文档模型的字段列表
     *
     * @param int $channelId
     * @return void
     */
    function updateFiledList ()
    {
        $Db = model('Db');
        
        $Db->query("SELECT field,emptyTip FROM @@__archive_field where channelId={$this->id}");
        
        $fieldList = '';
        
        while ($row = $Db->getNextRow())
        {
            if ($fieldList != '')
            {
                $fieldList .= empty($row->emptyTip) ? ",{$row->field}" : ",{$row->field}|{$row->emptyTip}";
            }
            else
            {
                $fieldList = empty($row->emptyTip) ? "{$row->field}" : "{$row->field}|{$row->emptyTip}";
            }
        }
        
        $Db->query("UPDATE @@__archive_channel set fieldList='{$fieldList}' where id = {$this->id}");
        
        $this->writeChannel();
    }

    public function __get ($key)
    {
        if (isset($this->$key))
        {
            return $this->$key;
        }
        else if (isset($this->data[$key]))
        {
            return $this->data[$key];
        }
        else
        {
            return '';
        }
    }
}